package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        int result = nursingProjectMapper.insert(nursingProject);
        //删除Redis中的缓存
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
        return result;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        int result = nursingProjectMapper.updateById(nursingProject);
        //删除Redis中的缓存
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
        return result;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        int result = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        //删除Redis中的缓存
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
        return result;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        int result = nursingProjectMapper.deleteById(id);
        //删除Redis中的缓存
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_ALL_KEY);
        return result;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        //1.查询Redis中的缓存，然后如果有直接返回
        String result = redisTemplate.opsForValue().get(CacheConstants.NURSING_PROJECT_ALL_KEY);
        if (StringUtils.isNotEmpty(result)){
            return JSON.parseArray(result,NursingProjectVo.class);
        }
        //2.如果没有，查询数据库
        List<NursingProjectVo> allProjects = nursingProjectMapper.getAllProjects();
        //3.将在数据库中查询到的数据存储到Redis缓存中
        redisTemplate.opsForValue().set(CacheConstants.NURSING_PROJECT_ALL_KEY,JSON.toJSONString(allProjects));
        //4.返回结果
        return allProjects;
    }

    /**
     * 小程序分页查询护理项目
     *
     * @param name     护理项目名称
     * @param pageNum  页码
     * @param pageSize 每页
     * @param status   状态
     * @return
     */
    @Override
    public TableDataInfo<NursingProject> pageNumAndPageSize(String name, Integer pageNum, Integer pageSize, Integer status) {
        Page<NursingProject> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<NursingProject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<NursingProject> wrapper = lambdaQueryWrapper.like(name != null ,NursingProject::getName, name)
                .eq(status != null ,NursingProject::getStatus, status);
        page = page(page,wrapper);
        return buildTableDateInfo(page);
    }

    private TableDataInfo<NursingProject> buildTableDateInfo(Page<NursingProject> page) {
        TableDataInfo<NursingProject> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

}
